package com.njuaplusplus.www.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.njuaplusplus.www.utils.Sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TestSudokuActivity extends Activity {
	/** Called when the activity is first created. */

	private Sudoku mSudoku = new Sudoku();
	private GridView mGridView;
	private int cellSelected;
	private String[] numberItems;
	private int[] sudokuArray = new int[81];
	private Button mButton_generateSudoku;
	private Button mBuuton_solveSudoku;
	private EditText mEditText_space;
	private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mGridView = (GridView) findViewById(R.id.gridView_sudoku);
		numberItems = getResources().getStringArray(R.array.numberItems);
		cellSelected = -1;
		Arrays.fill(sudokuArray, -1);
		mButton_generateSudoku = (Button)findViewById(R.id.button_generateSudoku);
		mBuuton_solveSudoku = (Button)findViewById(R.id.button_solveSudoku);
		mEditText_space = (EditText)findViewById(R.id.editText_space);
		
		mButton_generateSudoku.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("Generate space == " + mEditText_space.getText() + "  " + mEditText_space.toString());
				int space = Integer.parseInt(mEditText_space.getText().toString());
				System.out.println("space == " + space);
				
				sudokuArray = mSudoku.generateSudoku(space);
				setGridView();
			}
		});
		
		mBuuton_solveSudoku.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				solveSudoku();
			}
		});
		
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, final int position,
					long id) {
				// TODO Auto-generated method stub
				System.out.println("parent == " + parent);
				System.out.println("view == " + view);
				System.out.println("postion == " + position);
				System.out.println("id == " + id + " == " + parent.getItemIdAtPosition(position));
				System.out.println("data == " + parent.getItemAtPosition(position));
//				((TextView)view.findViewById(R.id.textView_cell)).setBackgroundColor(Color.BLUE);

				if (mSudoku.isHoles(position)) {

					new AlertDialog.Builder(TestSudokuActivity.this)
					.setTitle("Pick a number")
					.setItems(numberItems, new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							int tempNumber = sudokuArray[position];
							sudokuArray[position] = Integer.parseInt(numberItems[which]); 
							if (mSudoku.isCorrect(position, sudokuArray)) {
								Map<String, Object> map = new HashMap<String, Object>();
								map.put("NUMBER", numberItems[which]);
								listItems.set(position, map);
								refreshGridView();
								if (validateSudoku()) {
									Toast.makeText(getApplicationContext(), "Congratulations!", Toast.LENGTH_LONG).show();
								}
							}
							else {
								sudokuArray[position] = tempNumber;
								Toast.makeText(getApplicationContext(), "The number is wrong!", Toast.LENGTH_SHORT).show();
							}
						}
					})
					.show();
				}
			}

		});
		
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		for (int i=0; i<sudokuArray.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("NUMBER", (sudokuArray[i]==-1 ? " " : sudokuArray[i]));
			listItems.add(map);
		}

		refreshGridView();

	}

	private void refreshGridView() {
		mGridView.setAdapter(new SimpleAdapter(TestSudokuActivity.this, listItems, R.layout.cell, new String[] {"NUMBER"}, new int[] {R.id.textView_cell}));
	}
	
	private void setGridView() {
		listItems.clear();
		for (int i=0; i<sudokuArray.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("NUMBER", (sudokuArray[i]==-1 ? " " : sudokuArray[i]));
			listItems.add(map);
		}
		refreshGridView();
	}
	
	private boolean validateSudoku() {
		for (int i : sudokuArray) {
			if (i < 0) {
				return false;
			}
		}
		
		return true;
	}
	
	private void solveSudoku() {
		mSudoku.initSudokuMatrix(sudokuArray);
		mSudoku.solveSudoku();
		ArrayList<int[]> resultList = mSudoku.getResultList();
		if (resultList != null && resultList.size() > 0) {
			sudokuArray = resultList.get(0);
			setGridView();
		}
		else {
			Toast.makeText(getApplicationContext(), "No solution", Toast.LENGTH_SHORT).show();
		}
		
	}






}